<!--
	时间：2019-07-01
	描述：对账函融资报表
-->

<template>
	<div class="m-view-accountletterReport m-window">
		<!--查询条件（对账月份）-->
		<div class="m-condition">
			<el-row :gutter="10">
				<el-form
				 :model="ruleForm"
				 ref="ruleForm"
				 label-width="70px"
				 :inline-message="true"
				 label-position="left"
				>
					<el-col :span="7">
						<el-form-item label="月份" class="rightItem">
							<el-select
							 value-key="letterMonth"
							 v-model="letterMonth"
							 placeholder="请选择月份"
							 @change="focusInput('s_letterMonth')" ref="s_letterMonth"
							 clearable
							>
								<el-option
								 v-for="item in letterMonthOptions"
								 :key="item.letterMonth"
								 :label="item.letterMonth"
								 :value="item"
								>
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="2">
						<el-button type="primary" @click="searchReport(7)">查询</el-button>
					</el-col>
				</el-form>
			</el-row>
		</div>
		<!--对账函融资列表-->
		<div class="m-agTable m-bottom m-top2">
			 <ag-grid-vue  
			class="m-agTableContent ag-theme-balham" 
			:gridOptions="reportGridOptions" 
			:columnDefs="reportColumnDefs" 
			:rowData="rowData"  
			v-loading="loading" 
			rowSelection="single"
			></ag-grid-vue>
		</div>
		<!--分页-->
		<div class="m-page" v-if="diaShow">
			<el-pagination
				@size-change="reportHandleSizeChange"
				@current-change="reportHandleCurrentChange"
				@prev-click="reportHandlePrevClick"
				@next-click="reportHandleNextClick"
				:current-page="reportCurrentPage"
				:page-sizes="[30,50,100,150,200]"
				:page-size="reportPageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="reportTotal"
			></el-pagination>
		</div>
	</div>
</template>

<style lang="less">
    .m-view-accountletterReport{
       .el-form-item {
            margin-bottom: 0px;
        } 
    }
</style>

<script>
	import {formatPrice} from '@/util/format'
	import supplierInfo from "@/public/supplierInfo.js";
	import localeText from "@/i18n/agGridText";
	//import constant from "@/public/constant";
	import _APIS from "@/api";
    import gridOptions from 'lesso-common/public/gridOptions';
	export default {
		props: {

		},
		data() {
			return{
				ruleForm:{},
				letterMonth:'',
				letterMonthOptions:[],
				company:'',
				supplierId:this.$store.state.supplyChainFinance.custInf.custId, //获取供应商id
				//ag-grid配置
				reportGridOptions:'',
				reportColumnDefs: [],
				RowSelection: "multiple",
				rowData:[],
				loading:false,
				reportCurrentPage:1,//当前页码
				reportPageSize:30,//页容量
				reportTotal:0,//数据总量

				//缓存搜索条件
				tempSearch:{
					letterMonth:"",
					company:"",
					supplierId:"",
					selCusCode:"",
				},
				permission:'',
				diaShow:true,
				supplierInfo: [],
				//comOptions: [], //公司列表
			}
		},
		created() {
			//this.getAuthInfo()
			this.bindmonth()
			//this.searchReport(7)
		},
		beforeMount(){
			// 页面 ag-grid 配置
			this.reportGridOptions = Object.assign({
				context: {
					componentParent: this
				},
				enableColResize: true,
				localeText: localeText,
				enableCellChangeFlash: true,
				suppressRowClickSelection: true,
				enableFilter: true, //列过滤器
				enableSorting: true, //列排序
			},gridOptions())

			//配置列表字段
			this.reportColumnDefs = [
				{headerName: "公司编码", field: "companyCode", width:120},
				{headerName: "公司", field: "companyName", width:120},
				{headerName: "会计对账单号", field: "accountNo", width:120},
				{headerName: "对账函编号", field: "accountCode", width:120},
				{headerName: "对账月份", field: "yMonth", width:120},
				{headerName: "对账金额", field: "payBalance",valueFormatter: params => {return formatPrice(params.value)}, width:120},
				{headerName: "可使用融资金额", field: "borrowBalance",valueFormatter: params => {return formatPrice(params.value)}, width:120},
				{headerName: "已融资金额", field: "actBalance",valueFormatter: params => {return formatPrice(params.value)}, width:120},
				{headerName: "签署时间", field: "signComDate",valueFormatter: params => {return this.formatDate(params.value)}, width:120},
				{headerName: "签署状态", field: "signState", width:120},
				{headerName: "付款状态", field: "payFlag", width:120},
			]
		},
		mounted() {
			//this.searchReport(7)
		},
		methods: {
			getAuthInfo(){
				//获取当前账号权限公司
				var obj = {};
				var that = this
				//帐号类型：4为供应商
				if (this.$global.user.userTypeId == 4) {
					obj.account = this.$global.user.userName;
					obj.userType = 2;
					//this.supplierId = constant.supplierId;
					this.permission = obj.userType;
					if (!this.$global.supplierData) {
						supplierInfo.getUserInfo(this.$global.user.userName).then((supplierInfo) => {
							that.supplierInfo = supplierInfo;
						})
					} else {
						that.supplierInfo.push(this.$global.supplierData);
					}
				}
				_APIS.getAuthInfo(obj).then(function (response) {
					var data = response.data;
					if (data.code == 1) {
					//绑定公司选项
					if (data.rows.myMstCompany) that.comOptions = data.rows.myMstCompany;
					} else {
					that.comOptions = [];
					}
				}).catch((err) => {
					that.$alert(err.data.message, {
					showClose: false,
					confirmButton: "确定",
					customClass: "alertFailure"
					});
					that.comOptions = [];
				})
			},
			reportHandleSizeChange (params) {
				this.reportPageSize = params;
				this.getTempSearch();
				this.searchReport();
			},
			reportHandleCurrentChange (params) {
				this.reportCurrentPage = params;
				this.getTempSearch();
				this.searchReport();
			},
			reportHandlePrevClick () {
				this.reportCurrentPage = this.reportCurrentPage - 1;
				this.getTempSearch();
			},
			reportHandleNextClick () {
				this.reportCurrentPage += 1;
				this.getTempSearch();
			},
			//绑定月份
			bindmonth(){
				var that = this;
				_APIS.getApplyLetterList2({
					groupType:1,
					supplierId:that.supplierId,
					yMonth:that.letterMonth,
					current: that.reportCurrentPage,
                	size: that.reportPageSize,
				}).then(function success (response){
					var data = response.data;
					if(data.code == 200){
						if(data.total>0){
							data.rows.forEach(item=>{
								that.letterMonthOptions.push(item.yMonth)
							})
						}
					}else{
						that.$alert(data.info, {
							showClose: false,
							confirmButton: "确定",
							customClass: "alertFailure"
							}).then(() => {

						});
					}
				},function error(err){
					that.$alert("网络异常，查询失败！", {
						showClose: false,
						confirmButton: "确定",
						customClass: "alertFailure"
						}).then(() => {

					});
				})
			},
			//查询报表
			searchReport(type){
				var that = this;
				if(type === 7){
					this.reportCurrentPage = 1;
				}
				
				this.loading = true;
				that.saveTempSearch(); //缓存搜索条件

				_APIS.getApplyLetterList2({
					groupType:0,
					supplierId:that.supplierId,
					yMonth:that.letterMonth,
					current: that.reportCurrentPage,
                	size: that.reportPageSize,
				}).then(function success (response){
					var data = response.data;
					if(data.code == 200){
						that.rowData = data.rows;
						that.rowData.forEach(item=>{
                            if(item.signState === 1){
                                item.signState = '未签署';
                            }else if(item.signState === 2){
                                item.signState = '签署中';
                            }else{
								item.signState = '签署完成';
							}
							
							if(item.payFlag === 0){
                                item.payFlag = '未付款';
                            }else if(item.payFlag === 1){
                                item.payFlag = '部分付款';
                            }else{
								item.payFlag = '已付款';
							}

						})
						
						that.reportTotal = data.total;
                        that.loading = false;
					}else{
						that.$alert(data.info, {
							showClose: false,
							confirmButton: "确定",
							customClass: "alertFailure"
							}).then(() => {
							that.reportTotal = 0;
							that.loading = false;
						});
					}
				},function error(err){
					that.$alert("网络异常，查询失败！", {
						showClose: false,
						confirmButton: "确定",
						customClass: "alertFailure"
						}).then(() => {
						that.reportTotal = 0;
						that.loading = false;
					});
				})
			},

			//缓存查询条件
			saveTempSearch(){
				this.tempSearch.company = this.company.companyCode;
				this.tempSearch.supplierId = this.supplierId;
				this.tempSearch.letterMonth = this.letterMonth;
			},

			//读取缓存查询数据
			getTempSearch(){
				this.company.companyCode = this.tempSearch.company;
				this.supplierId = this.tempSearch.supplierId;
				this.letterMonth = this.tempSearch.letterMonth;
			},

			//时间截止到天
			formatDate(time) {
				if (time) {
					return time.substring(0, 10)
				} else {
					return ''
				}
			},

			focusInput(refName){
				if(refName === 's_letterMonth'){
				this.searchReport(7)
				return
				}
				this.$refs[refName].focus()
			}
		},
		components: {

		},
		watch: {

		},

	}
</script>